import { NTimelineItem, type TimelineItemProps } from "naive-ui";
import type { HTMLAttributes } from "vue";
import type { BaseComponentType } from "../types";
import { getSlotsDom } from "../utils";
export type TimelineItemFactoryConstructorType = {
  props?: TimelineItemProps;
  attrs?: HTMLAttributes;
  defaultSlot?: BaseComponentType;
  iconSlot?: BaseComponentType;
  footerSlot?: BaseComponentType;
  headerSlot?: BaseComponentType;
};

export class TimelineItemFactory {
  private attrs: HTMLAttributes = {};
  private props: TimelineItemProps = {};
  private defaultSlot: BaseComponentType = null;
  private iconSlot: BaseComponentType = null;
  private footerSlot: BaseComponentType = null;
  private headerSlot: BaseComponentType = null;

  constructor(data?: TimelineItemFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.defaultSlot) this.setDefault(data.defaultSlot);
    if (data?.iconSlot) this.setIcon(data.iconSlot);
    if (data?.footerSlot) this.setFooter(data.footerSlot);
    if (data?.headerSlot) this.setHeader(data.headerSlot);
  }

  setAttrs(attrs: HTMLAttributes) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: TimelineItemProps) {
    this.props = props;
    return this;
  }

  setDefault(component: BaseComponentType) {
    this.defaultSlot = component;
    return this;
  }

  setIcon(component: BaseComponentType) {
    this.iconSlot = component;
    return this;
  }

  setFooter(component: BaseComponentType) {
    this.footerSlot = component;
    return this;
  }

  setHeader(component: BaseComponentType) {
    this.headerSlot = component;
    return this;
  }

  create() {
    return (
      <NTimelineItem {...this.attrs} {...this.props}>
        {{
          default:
            this.defaultSlot != null
              ? () => getSlotsDom(this.defaultSlot as BaseComponentType)
              : null,

          icon:
            this.iconSlot != null
              ? () => getSlotsDom(this.iconSlot as BaseComponentType)
              : null,

          footer:
            this.defaultSlot != null
              ? () => getSlotsDom(this.footerSlot as BaseComponentType)
              : null,
          header:
            this.defaultSlot != null
              ? () => getSlotsDom(this.headerSlot as BaseComponentType)
              : null,
        }}
      </NTimelineItem>
    );
  }
}
